var pwdRegex = new RegExp("(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,20}");

var phoneNum;
var vCode;
var newpwd;
var pwdagain;
var loadding;

var t;
var countdown = 60;

function setEnable() {
    $("#getvcode").html("获取验证码");
    $("#getvcode").prop("href", "javascript:getCode();");
    $("#getvcode").removeClass("a-gray");
    $("#getvcode").addClass("a-blue");
}

function setDisable() {
    $("#getvcode").html(countdown + " 秒");
    $("#getvcode").prop("href", "javascript:");
    $("#getvcode").removeClass("a-blue");
    $("#getvcode").addClass("a-gray");
}

function gettingCode() {
    if (countdown == 0) {
        setEnable();
        countdown = 60;
        return;
    } else {
        setDisable();
        countdown--;
    }
    t = setTimeout(function () {
        gettingCode();
    }, 1000)
}

function getCode() {
    phoneNum = $("#phoneNum").val();
    var regex_phoneNum = /^\d{11}$/;
    if (!regex_phoneNum.test(phoneNum)) {
        layer.open({
            content: '请输入正确的手机号码'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
        return;
    }

    

    loadding = layer.open({type: 2});
    getGeetest({
        loginname:phoneNum, 
        mark:'repwd'
    }).then(function(){
        layer.close(loadding)
        return getSmsCode({
            phone: phoneNum,
            typeCode: 'RePwd',
            mark: 'repwd'
        })
    }).then(function(res){
        layer.open({
            content: '手机验证码已发送'
            ,skin: 'msg'
            ,time: 2
        });
        gettingCode();
    })
}

function valid() {
    newpwd = $("#newpwd").val();
    pwdagain = $("#pwdagain").val();

    if (!pwdRegex.test(newpwd)) {

        layer.open({
            content: '须包含字母,数字,特殊字符,8-20位'
            ,skin: 'msg'
            ,time: 2
        });
        return false;
    }

   
    if (newpwd != pwdagain) {
      
        layer.open({
                content: '两次输入的新密码不一致'
                ,skin: 'msg'
                ,time: 2
            });
        return false;
    }
    return true;
}

function submit() {
    if (valid()) {
        
        var cellPhoneNum = $.trim($('#phoneNum').val())
        var vCode = $.trim($('#vCode').val())
        var newPwd = $.trim($('#newpwd').val())
     
        
        loadding = layer.open({type: 2});

        $.ajax({
            url: baseUrl + "/bdc/api/app/login/regainPassword",
            type: "post",
          
            data:{
                cellPhoneNum: cellPhoneNum,
                vCode: vCode,
                newPwd: newPwd
            },
            xhrFields: {
    　　　　　　withCredentials: true
    　　　　},
            crossDomain: true,
            dataType: "json",
            success: function (res) { 
                layer.close(loadding)
                if (res.status == 200) {
                  
                    var href = location.href
                    href = href.replace('resetPassword', 'signUp')
                    window.parent.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+href+"&response_type=code&scope="+scope+"&state=123&connect_redirect=1#wechat_redirect";
                    
                }else {
                    layer.open({
                        content: res.message
                        ,skin: 'msg'
                        ,time: 2
                    });
                }
                
            },
            error: function () {
                layer.close(loadding)
               
            }
        });
    }
}

